package com.ywk.effective._16_composition;

import java.util.Collection;
import java.util.Date;
import java.util.Set;
import java.util.TreeSet;

public class InstrumentedSet<E> extends ForwardingSet<E> {
    private int addCount =0;
    public InstrumentedSet(Set<E> s) {
        super(s);
    }

    @Override
    public boolean add(E e) {
        addCount++;
        return super.add(e);
    }

    @Override
    public boolean addAll(Collection c) {
        addCount += c.size();
        return super.addAll(c);
    }

    public int getAddCount() {
        return addCount;
    }
    static class Main{
        public static void main(String[] args) {
            TreeSet<Date> dates = new TreeSet<>();
            dates.add(new Date());
            dates.add(new Date(1));
            dates.add(new Date(2));
            Set<Date> iDogs = new InstrumentedSet<Date>(new TreeSet<>());
            iDogs.addAll(dates);
            System.out.println(((InstrumentedSet<Date>) iDogs).getAddCount());
            System.out.println(iDogs.size());
        }
    }
}
